<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="收货人名称:">
        <el-input v-model="queryParams.skuName" size="small" placeholder="请输入收货人名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-popover placement="bottom-start" width="460" trigger="click">
          <sku-selector ref="skuSelector" :query-params="{customerId: customer.id}" @selected="handleSelectedSku($event)" />
          <el-button slot="reference" type="primary" icon="el-icon-plus" size="mini">选择</el-button>
        </el-popover>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="openUploadDialog">导入</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row>
      <el-table-column label="客户名称" align="center" prop="customerName" width="180" />
      <el-table-column label="收货人名称" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.materialNo" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.materialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货人手机号" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.materialNo" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.materialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货人城市" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.materialNo" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.materialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货人地址" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.materialNo" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.materialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed>
        <template slot-scope="{row}">
          <el-button v-if="row.edit" type="success" size="small" icon="el-icon-circle-check-outline" @click="handleEdit(row)">确认</el-button>
          <el-button v-else type="primary" size="small" icon="el-icon-edit" @click="row.edit=!row.edit">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 导入对话框 -->
    <el-dialog :title="uploadDialog.title" :visible.sync="uploadDialog.visible" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        :headers="uploadData.headers"
        :action="uploadData.url + '?updateSupport=' + uploadData.updateSupport"
        :data="uploadData.params"
        :disabled="uploadData.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        accept=".xlsx, .xls"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div slot="tip" class="el-upload__tip">
          <el-link type="info" style="font-size:12px" @click="handleDownloadTemplate">下载模板</el-link>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:red">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmUploadDialog">确 定</el-button>
        <el-button @click="resetUploadDialog">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import TWhWarehouseCustomerMaterialApi from '@/api/pms/TWhWarehouseCustomerMaterialApi'
import AuthUtils from '@/utils/AuthUtils'
import SkuSelector from '@/views/goods/sku/SkuSelector.vue'

const defaultFormData = {
  customerId: null,
  customerName: null,
  materialNo: null,
  shipByPackOrQty: null,
  unitWeight: null,
  unitVolume: null,
  packPerPallet: null,
  qtyPerPack: null,
  needrRePackYn: null,
  needPalletYn: null,
  needLiftYn: null
}
export default {
  name: 'Consignee',
  components: {
    SkuSelector
  },
  props: {
    customer: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  watch: {
    customer() {
      this.$refs.skuSelector.handleQuery()
      this.handleQuery()
    }
  },
  data() {
    return {
      options: {
        materialOptions: [],
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        customerNo: this.customer.id,
        skuName: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      loading: true,
      searchVisible: true,
      uploadDialog: {
        title: '批量导入',
        visible: false
      },
      uploadData: {
        headers: { Authorization: 'Bearer ' + AuthUtils.getToken() }, // 上传请求头
        url: process.env.VUE_APP_BASE_API + '/order/salesOrderItem/importData', // 上传的地址
        params: {
          customerNo: null
        },
        isUploading: false, // 是否禁用上传
        updateSupport: 0 // 是否更新已经存在的数据
      },
      skuSelectorVisible: false
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.page.records.push({
        ...defaultFormData,
        customerId: this.customer.id,
        customerName: this.customer.customerName,
        edit: true
      })
    },
    handleRemove(row) {
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return TWhWarehouseCustomerMaterialApi.remove(row.id)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      if (row.id) {
        TWhWarehouseCustomerMaterialApi.edit(row).then(response => {
          this.$message.success('修改成功')
          this.handleQuery()
        })
      } else {
        TWhWarehouseCustomerMaterialApi.add(row).then(response => {
          this.$message.success('添加成功')
          this.handleQuery()
        })
      }
    },
    handleQuery() {
      this.loading = true
      if (this.customer.id) {
        /* TWhWarehouseCustomerMaterialApi.page(this.queryParams, this.page).then(response => {
          this.page.records = response.data.records
          this.page.total = response.data.total
          this.loading = false
        }) */
        this.loading = false
      }
    },
    handleSelectedSku(item) {
      this.page.records.push({
        materialNo: item.skuCode,
        shipByPackOrQty: null,
        unitWeight: null,
        unitVolume: null,
        packPerPallet: null,
        qtyPerPack: null,
        needrRePackYn: null,
        needPalletYn: null,
        needLiftYn: null,
        customerId: this.customer.id,
        customerName: this.customer.customerName,
        edit: true
      })
    },
    confirmUploadDialog() {
      this.$refs.upload.submit()
    },
    resetUploadDialog() {
      this.uploadDialog.visible = false
      //this.uploadData.params.customerNo = null
    },
    openUploadDialog() {
      this.resetUploadDialog()
      this.uploadDialog.visible = true
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.uploadData.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.uploadDialog.visible = false
      this.uploadData.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.data ? response.data : response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.queryParams.status = null
      this.handleQuery()
    },
    handleDownloadTemplate(){

    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
